package com.ly.im.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ly.im.pojo.Message;
import com.ly.im.pojo.User;
import com.ly.im.repository.MessageRepository;
import com.ly.im.service.MessageService;
import com.ly.im.utils.ResponseUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author LY
 * @create 2024/2/2 16:14
 * @Description 消息
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageRepository, Message> implements MessageService {

    @Override
    public Object list(User user, Long to, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Map> list = baseMapper.list(user.getId(), to);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("total", PageInfo.of(list).getTotal());
        return ResponseUtils.ok(map);
    }

    @Override
    public Object addMessage(Message message) {
        message.setType(1);
        message.setTime(new Date());
        baseMapper.saveMessage(message);
        return ResponseUtils.ok();
    }
}
